package com.gift.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gift.auto.JSONAuthentication;
import com.gift.entity.GiLpc;
import com.gift.entity.LpcProduct;
import com.gift.entity.LpcType;
import com.gift.entity.ProductOrder;
import com.gift.mapper.*;
import com.gift.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gift
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/product-order")
public class ProductOrderController extends JSONAuthentication {
    @Autowired
    ProductOrderMapper productOrderMapper;
    @Autowired
    GiLpcMapper giLpcMapper;
    @Autowired
    LpcTypeMapper lpcTypeMapper;
    @Autowired
    LpcUserMapper lpcUserMapper;
    @Autowired
    LpcProductMapper lpcProductMapper;
    @Autowired
    GiProductMapper giProductMapper;
    @ApiOperation(value = "使用兑换码兑换商品生成订单")
    @PostMapping("/add")
    public void add(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId,@RequestParam("proId") int proId,@RequestParam("lpcNum") long lpcNum,@RequestParam("lpcPassword") long lpcPassword ) throws Exception {
       QueryWrapper wrapper=new QueryWrapper();
       wrapper.eq("lp_num",lpcNum);
       if (giLpcMapper.selectOne(wrapper)!=null){
           GiLpc giLpc = giLpcMapper.selectOne(wrapper);
           if(Objects.equals(lpcPassword, giLpc.getPassword())||giLpc.getStatus()=="已失效"){
               int typeId = giLpc.getTypeId();
               LpcType lpcType = lpcTypeMapper.selectById(typeId);
               int price=lpcType.getPrice();
               QueryWrapper wrapper1=new QueryWrapper();
               wrapper1.eq("price",price);
               List<LpcProduct> list= lpcProductMapper.selectList(wrapper1);
               int flag=0;
               for(LpcProduct a:list){
                   if(a.getPrId()==proId){
                       flag=1;
                   }
               }
               if(flag==1){
                   QueryWrapper wrapper2=new QueryWrapper();
                   wrapper2.eq("lpc_id",giLpc.getId());
                   ProductOrder productOrder=new ProductOrder();
                   productOrder.setUserId(userId);
                   productOrder.setProId(proId);
                   productOrder.setLpcNum(lpcNum);
                   Date date=new Date();
                   Timestamp timestamp =new Timestamp(date.getTime());
                   productOrder.setCreatTime(timestamp);
                   giLpc.setStatus("已兑换");
                   if(productOrderMapper.insert(productOrder)>0&&lpcUserMapper.delete(wrapper2)>0&&giLpcMapper.updateById(giLpc)>0){
                       Result ok = Result.ok().message("兑换成功");
                       this.WriteJSON(request, response, ok);
                   }else{
                       Result error = Result.error().message("兑换失败");
                       this.WriteJSON(request, response, error);
                   }

               }else{
                   Result error = Result.error().message("兑换商品错误");
                   this.WriteJSON(request, response, error);
               }

           }else{
               Result error = Result.error().message("密码错误或兑换码已失效");
               this.WriteJSON(request, response, error);
           }

       }else{
           Result error = Result.error().message("礼品册号码不存在");
           this.WriteJSON(request, response, error);
       }
    }
    @ApiOperation(value = "删除订单")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = productOrderMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "发货订单")
    @PostMapping("/updFh")
    public void updFh(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("num") String num) throws Exception {
        ProductOrder lpcOrder = productOrderMapper.selectById(id);
        if(lpcOrder.getStatus().equals("待发货")){
            lpcOrder.setStatus("运送中");
            lpcOrder.setNumm(num);
           if (productOrderMapper.updateById(lpcOrder)>0) {
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }else{
            Result error = Result.error().message("订单信息错误");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "订单到达")
    @PostMapping("/updDd")
    public void updDd(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("num") String num) throws Exception {
        ProductOrder lpcOrder = productOrderMapper.selectById(id);
        if(lpcOrder.getStatus().equals("运送中")){
            lpcOrder.setStatus("已到达");
            lpcOrder.setNumm(num);
            if (productOrderMapper.updateById(lpcOrder)>0) {
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }else{
            Result error = Result.error().message("订单信息错误");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "订单签收")
    @PostMapping("/updQs")
    public void updQs(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("num") String num) throws Exception {
        ProductOrder lpcOrder = productOrderMapper.selectById(id);
        if(lpcOrder.getStatus().equals("已到达")){
            lpcOrder.setStatus("已签收");
            lpcOrder.setNumm(num);
            if (productOrderMapper.updateById(lpcOrder)>0) {
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }else{
            Result error = Result.error().message("订单信息错误");
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查看所有订单")
    @PostMapping("/findAllLOrder")
    public void findAllLOrder(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<ProductOrder> shUserPage=new Page<>(current,size);
        Page<ProductOrder> shUserPages=productOrderMapper.selectPage(shUserPage,null);
        List<ProductOrder> records = shUserPages.getRecords();
        int total=productOrderMapper.selectCount(null);
        for(ProductOrder a:records){
        a.setGiProduct(giProductMapper.selectById(a.getProId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("Order",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查看所有待发货订单")
    @PostMapping("/findAllLOrderNo")
    public void findAllLOrderNo(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","待发货");
        Page<ProductOrder> shUserPage=new Page<>(current,size);
        Page<ProductOrder> shUserPages=productOrderMapper.selectPage(shUserPage,wrapper);
        List<ProductOrder> records = shUserPages.getRecords();
        for(ProductOrder a:records){
            a.setGiProduct(giProductMapper.selectById(a.getProId()));
        }
        int total=productOrderMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("Order",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查看所有运送中订单")
    @PostMapping("/findAllLOrderYs")
    public void findAllLOrderYs(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","运送中");
        Page<ProductOrder> shUserPage=new Page<>(current,size);
        Page<ProductOrder> shUserPages=productOrderMapper.selectPage(shUserPage,wrapper);
        List<ProductOrder> records = shUserPages.getRecords();
        for(ProductOrder a:records){
            a.setGiProduct(giProductMapper.selectById(a.getProId()));
        }
        int total=productOrderMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("Order",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查看所有已到达订单")
    @PostMapping("/findAllLOrderDd")
    public void findAllLOrderDd(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","已到达");
        Page<ProductOrder> shUserPage=new Page<>(current,size);
        Page<ProductOrder> shUserPages=productOrderMapper.selectPage(shUserPage,wrapper);
        List<ProductOrder> records = shUserPages.getRecords();
        for(ProductOrder a:records){
            a.setGiProduct(giProductMapper.selectById(a.getProId()));
        }
        int total=productOrderMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("Order",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查看所有已签收订单")
    @PostMapping("/findAllLOrderQs")
    public void findAllLOrderQs(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","已签收");
        Page<ProductOrder> shUserPage=new Page<>(current,size);
        Page<ProductOrder> shUserPages=productOrderMapper.selectPage(shUserPage,wrapper);
        List<ProductOrder> records = shUserPages.getRecords();
        for(ProductOrder a:records){
            a.setGiProduct(giProductMapper.selectById(a.getProId()));
        }
        int total=productOrderMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("Order",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "根据用户id查询订单")
    @PostMapping("/findAllLOrderQsByUserId")
    public void findAllLOrderQsByUserId(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam ("current") int current,@RequestParam("userId") int userId)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<ProductOrder> shUserPage=new Page<>(current,size);
        Page<ProductOrder> shUserPages=productOrderMapper.selectPage(shUserPage,wrapper);
        List<ProductOrder> records = shUserPages.getRecords();
        for(ProductOrder a:records){
            a.setGiProduct(giProductMapper.selectById(a.getProId()));
        }
        int total=productOrderMapper.selectCount(wrapper);

        if(records.size()>0){
            Result ok = Result.ok().data("Order",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

